<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataview-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Using Automatic Paging with a DataGrid Control</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}

.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
        font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
        font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}

</style></head>
<body bgcolor="#ffffff">
<span class="heading">Using Automatic Paging with a DataGrid Control</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<%'-- insert the control that creates the DataSet --%>
<wrox:getdataview id="ctlDataView" runat="server" />

<form runat="server">

   'Previous' Link Text:
   <ASP:TextBox id="PrevText" runat="server" /> &nbsp;
   'Next' Link Text:
   <ASP:TextBox id="NextText" runat="server" /><br />

   Paging Style:
   <ASP:RadioButton id="PageNumeric" GroupName="Style"
                    runat="server" /> Numeric &nbsp;
   <ASP:RadioButton id="PageText" GroupName="Style"
                    runat="server" /> Text<br />

   Number of rows per Page:
   <ASP:TextBox id="PageRows" runat="server" /><p />

   <ASP:DataGrid id="MyDataGrid" runat="server"
        Width="90%"
        AllowPaging="True"
        PagerStyle-HorizontalAlign="Right"
        OnPageIndexChanged="ChangeGridPage" />

</form>

<!--------------------------------------------------------------------------->

<script language="vb" runat="server">

Sub Page_Load()
   If Not Page.IsPostback Then

      'set the default values in the controls on the page
      PrevText.Text = "Prev"
      NextText.Text = "Next"
      PageNumeric.Checked = True
      PageRows.Text = "10"

      'set the initial page in the DataGrid to zero
      '(not actually required as this is the default)
      MyDataGrid.CurrentPageIndex = 0

      'create the data set and bind it to the DataGrid control
      BindDataGrid()

   End If
End Sub


Sub ChangeGridPage(objSender As Object, objArgs As DataGridPageChangedEventArgs)
   'runs when one of the pager controls is clicked

   'update the current page number from the parameter values
   MyDataGrid.CurrentPageIndex = objArgs.NewPageIndex

   'recreate the data set and bind it to the DataGrid control
   BindDataGrid()

End Sub


Sub BindDataGrid()

   'set the value of the number of rows per page
   MyDataGrid.PageSize = CInt(PageRows.Text)

   'set the type of pager to include in the DataGrid
   If PageNumeric.Checked = True Then
      MyDataGrid.PagerStyle.Mode = PagerMode.NumericPages
   Else
      MyDataGrid.PagerStyle.Mode = PagerMode.NextPrev
   End If

   'set the text for the pager to use when in NextPrev mode
   MyDataGrid.PagerStyle.NextPageText = NextText.Text
   MyDataGrid.PagerStyle.PrevPageText = PrevText.Text

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect As String  = ctlConnectStrings.OLEDBConnectionString

   'create a SQL statement to select some rows from the database
   Dim strSelect As String
   strSelect = "SELECT * FROM BookList"

   'create a variable to hold an instance of a DataView object
   Dim objDataView As DataView

   'get DataView from get-dataview-control.ascx user control
   objDataView = ctlDataView.GetDataView(strConnect, strSelect)

   If IsNothing(objDataView) Then Exit Sub

   'set the DataSource property of the DataList
   MyDataGrid.DataSource = objDataView

   'and bind the control to the data
   MyDataGrid.DataBind()

End Sub

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
&copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>
